package com.cqc.customslidmenu01;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Scroller;

/**
 * Created by a on 2018/3/16.
 * 侧滑菜单
 */

public class SlideView2 extends ViewGroup {


    private View menuView;
    private View mainView;
    private int menuViewWidth;
    private int downX;
    private int moveX;
    private boolean menuIsOpen = false;//默认没有打开menu
    private Scroller scroller;

    public SlideView2(Context context) {
        this(context, null);
    }

    public SlideView2(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SlideView2(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        scroller = new Scroller(getContext());
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        menuView = getChildAt(0);
        menuViewWidth = menuView.getLayoutParams().width;
        menuView.measure(menuViewWidth, heightMeasureSpec);

        mainView = getChildAt(1);
        mainView.measure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        //l t r b 是SlideView的，不是某一个childView,   0  0  720  1024
//        LogUtil.d("l=" + l + ",t=" + t + ",r=" + r + ",b" + b);
        menuView.layout(-menuViewWidth, t, 0, b);
        mainView.layout(l, t, r, b);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                moveX = (int) event.getX();
                int instance = downX - moveX;
//                scrollTo(getScrollX()+instance,0);//错误，因为downX的值一直没变
//                scrollBy(instance, 0);
//                downX = moveX;
                scrollTo(instance, 0);

//                int scrollX = getScrollX() + instance;//[-menuViewWidth,0]
//                if (scrollX > 0) {
//                    scrollTo(0, 0);
//                } else if (scrollX < -menuViewWidth) {
//                    scrollTo(-menuViewWidth, 0);
//                } else {
//                    scrollBy(instance, 0);
//                }
//                downX = moveX;
                break;
            case MotionEvent.ACTION_UP:
                //判断有没有过了 menuView的1/2
                int scrollX1 = Math.abs(getScrollX());
                int center = menuViewWidth / 2;
                if (scrollX1 > center) {
                    LogUtil.d("关闭menu");
//                    scrollTo(-menuViewWidth, 0);
//                    menuIsOpen = false;

                    menuIsOpen = true;
                } else {
                    LogUtil.d("打开menu");
//                    scrollTo(0, 0);
//                    menuIsOpen = true;

                    menuIsOpen = false;
                }

//                switchMenu2();
                break;
        }
//        LogUtil.d("downX=" + downX);
        return true;
    }


    /**
     * 提供对外调用方法
     */
    public void switchMenu() {
        if (menuIsOpen) {
            scrollTo(-menuViewWidth, 0);
            menuIsOpen = false;
        } else {
            scrollTo(0, 0);
            menuIsOpen = true;
        }
    }


    public void switchMenu2() {
        int startX = getScrollX();
        int dx;
        if (menuIsOpen) {
            dx = -menuViewWidth - startX;
            menuIsOpen = false;
        } else {
            dx = 0 - startX;
            menuIsOpen = true;
        }
        //dx = endX - startX;
        scroller.startScroll(startX, 0, dx, 0, Math.abs(dx) * 5);
        invalidate();
    }

    @Override
    public void computeScroll() {
        super.computeScroll();
        if (scroller.computeScrollOffset()) {
            scrollTo(scroller.getCurrX(), 0);
            invalidate();
        }
    }


    /**
     * 没有作用
     * @param event
     * @return
     */
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                downX = (int) event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                //移动距离<8不做处理
                moveX = (int) event.getX();
                if (Math.abs(downX - moveX) > 200) {
                    return true;
                }
                break;
            default:
                break;
        }
        return super.onInterceptTouchEvent(event);
    }

}
